﻿using System;
using System.Web.UI;
using ZN.Common;

namespace ZN.Web.Controls {

    /// <summary>
    /// 日期范围选择控件.
    /// </summary>
    public partial class DatePicker : System.Web.UI.UserControl {

        #region "私有成员"

        private DatePickerSkin _Sikn = DatePickerSkin.Default;

        #endregion

        #region "构造函数"

        public DatePicker() {
            CssClass = "Wdate";
            FormatString = "yyyy-MM-dd";
            ActiveEvent = "click";
        }

        #endregion

        #region "公共属性"

        #region "外观"
        /// <summary>
        /// 获取或设置控件皮肤外观
        /// </summary>
        public DatePickerSkin Skin {
            get { return _Sikn; }
            set { _Sikn = value; }
        }
        /// <summary>
        /// 绑定控件样式
        /// </summary>
        public string CssClass {
            get;
            set;
        }

        #endregion

        #region "行为"

        /// <summary>
        /// 日期字符串格式
        /// </summary>
        public string FormatString { get; set; }
        /// <summary>
        /// 激活控件事件,默认为Click
        /// </summary>
        public string ActiveEvent { get; set; }
        /// <summary>
        /// 日期选中后事件
        /// </summary>
        public string OnClientSelected { get; set; }
        /// <summary>
        /// 获取绑定后控件1的值
        /// </summary>
        public DateTime? AssociatedControl1Date {
            get {
                if (Request.Params[AssociatedControl1ID] == null)
                    return null;
                else {
                    DateTime dt = DateTime.MinValue;
                    if (DateTime.TryParse(Request.Params[AssociatedControl2ID], out dt))
                        return dt;
                    return null;
                }
            }
        }
        /// <summary>
        /// 获取绑定后控件2的值
        /// </summary>
        public DateTime? AssociatedControl2Date {
            get {
                if (Request.Params[AssociatedControl2ID] == null)
                    return null;
                else {
                    DateTime dt = DateTime.MinValue;
                    if (DateTime.TryParse(Request.Params[AssociatedControl2ID], out dt))
                        return dt;
                    return null;
                }
            }
        }
        /// <summary>
        /// 绑定相关控件1
        /// </summary>
        public string AssociatedControl1ID {
            get;
            set;
        }
        /// <summary>
        /// 绑定相关控件2
        /// </summary>
        public string AssociatedControl2ID {
            get;
            set;
        }
        public bool AllowDealyLoaded {
            get;
            set;
        }
        public bool CustomCreateOnly {
            get;
            set;
        }
        #endregion

        #endregion

        #region "页面事件"
        //页面载入
        protected void Page_Load(object sender, EventArgs e) {
            //如果控件可见,则初始化脚本
            if (this.Visible) InitScripts();

        }
        protected override void Render(HtmlTextWriter writer) {
            base.Render(writer);
            string sScript=string.Empty;
            if (!CustomCreateOnly) {
                sScript = string.Format(@"<script type='text/javascript'>var {0}; {8}  {0}=new DatePicker({{AssociatedControl1ID:'{1}',
                                                                                                     AssociatedControl2ID:'{2}',
                                                                                                     FormatString:'{3}',
                                                                                                     Skin:'{4}',
                                                                                                     ActiveEvent:'{5}',
                                                                                                     CssClass:'{6}',
                                                                                                     OnSeleted:{7} {9}}});</script>"
                                                                                                       , this.ID,
                                                                                                       AssociatedControl1ID,
                                                                                                       AssociatedControl2ID,
                                                                                                       FormatString,
                                                                                                       Skin.ToString().ToLower(),
                                                                                                       ActiveEvent,
                                                                                                       CssClass,
                                                                                                       string.IsNullOrEmpty(OnClientSelected) ? "function(){}" : OnClientSelected,
                                                                                                       AllowDealyLoaded ? "$(document).ready(function(){\n" : "",
                                                                                                       AllowDealyLoaded ? "});\n" : ""
              );
            }
            writer.Write(sScript.Replace("\n", ""));
        }

        #endregion

        #region "私有方法"

        /// <summary>
        /// 注册脚本
        /// </summary>
        private void InitScripts() {
            JavaScript.IncludeJs(Page, "WdatePicker.js", string.Format("{0}/../Scripts/datepicker/WdatePicker.js", TemplateSourceDirectory));
            JavaScript.IncludeJs(Page, "DatePicker.ascx.js", string.Format("{0}/../Scripts/DatePicker.ascx.js", TemplateSourceDirectory));
        }

        #endregion

    }

    /// <summary>
    /// 日期控件显示样式
    /// </summary>
    public enum DatePickerSkin {
        /// <summary>
        /// 默认样式
        /// </summary>
        Default,
        /// <summary>
        /// 简单
        /// </summary>
        Simple,
        /// <summary>
        /// 红色
        /// </summary>
        Red,
        /// <summary>
        /// 蓝色
        /// </summary>
        Blue,
        /// <summary>
        /// 
        /// </summary>
        BlueFresh,
        /// <summary>
        /// 绿色
        /// </summary>
        Green
    }
}